<?php
namespace app\model;

require EXTEND_PATH.'alipay/pagepay/service/AlipayTradeService.php';
require EXTEND_PATH.'alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';

use AlipayTradeService;
use AlipayTradePagePayContentBuilder;
use think\Config;
class Pay
{
    function __construct() {
    }

    /*
     * 支付宝支付
     * $type=1支付
     * $out_trade_no=商户订单号，商户网站订单系统中唯一订单号，必填
     * $subject=订单名称，必填
     * $total_amount=付款金额，必填
     * $body=//商品描述，可空
    */
    public function alipay($type=1,$out_trade_no,$subject,$total_amount,$body){
        if($type==1){
            Config::load(APP_PATH.'other.php');
            $config = Config::get('alipay');//获取支付宝支付配置
            //构造参数
            $payRequestBuilder = new AlipayTradePagePayContentBuilder();
            $payRequestBuilder->setBody($body);
            $payRequestBuilder->setSubject($subject);
            $payRequestBuilder->setTotalAmount($total_amount);
            $payRequestBuilder->setOutTradeNo($out_trade_no);
            $aop = new AlipayTradeService($config);

            /**
             * pagePay 电脑网站支付请求
             * @param $builder 业务参数，使用buildmodel中的对象生成。
             * @param $return_url 同步跳转地址，公网可以访问
             * @param $notify_url 异步通知地址，公网可以访问
             * @return $response 支付宝返回的信息
             */
            $response = $aop->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);
        }
    }

    public function alipay_notify(){
        Config::load(APP_PATH.'other.php');
        $config = Config::get('alipay');//获取支付宝支付配置
        $arr=$_POST;
        $alipaySevice = new AlipayTradeService($config);
        $alipaySevice->writeLog(var_export($_POST,true));
        return $alipaySevice->check($arr);
    }



}

